import java.util.Arrays;

/**
 * @ClassName Code01_swap
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/12/4 21:02
 */

// 异或交换两个数
public class Code01_Swap {

    /**
     * swapNum 交换两个基本数据类型的值，不会发生置为 0 的情况
     */
    public static void swapNum() {
        int a = 88;
        int b = 100;
        System.out.println("a: " + a);
        System.out.println("b: " + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println("a: " + a);
        System.out.println("b: " + b);
    }

    /**
     * swapArray 交换数组中的两个数，要求传入的位置不能一样，要不然那个位置会变为 0（因为数组是引用传参）
     *
     * @param array
     * @param i
     * @param j
     */
    public static void swapArray(int[] array, int i, int j) {
        System.out.println(Arrays.toString(array));
        array[i] = array[i] ^ array[j];
        array[j] = array[i] ^ array[j];
        array[i] = array[i] ^ array[j];
        System.out.println(Arrays.toString(array));
    }

    public static void main(String[] args) {
        swapNum();
        swapArray(new int[]{10, 20, 30, 40, 50}, 1, 2);
        swapArray(new int[]{10, 20, 30, 40, 50}, 1, 1);
    }
}
